<?php

/*

function update_table(){
	for($n =0; $n <=7; $n++){ 
	$bulan = sprintf('%02d',$n);
	$query = "SELECT * FROM a_surat_ijin WHERE MONTH(tanggal) = '{$bulan}' ORDER BY tanggal ASC";
	$result = my_query($query);
	while($t = my_fetch_array($result)){
		$nomor = getNumber('2011',$bulan);
		$datas = array(
			'nomor_surat'=>my_type_data_str($nomor)
		);
		my_update_record('a_surat_ijin','surat_ijin_id',$t['surat_ijin_id'],$datas);
	} 
	}
}
*/

function getNumber($tahun,$bulan){
	$query = "SELECT max(nomor_surat) as nn FROM a_surat_ijin 
		WHERE nomor_surat LIKE '%{$tahun}{$bulan}%' ";
	$res = my_query($query);
	$row = my_fetch_array($res); 
 
	return $tahun.$bulan.sprintf('%05d',(int)substr($row['nn'] ,6,12) + 1);
}
//===========================================




function list_ijin_all(){
my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor'=>array('style'=>'border-bottom:2px solid;width:12%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:8%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:27%'),  
		'Full Day'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'),  
		'Rekomendasi'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'),  
		'Dari'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'),  
		'Sampai'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'),  
		'Keterangan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:14%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
		$query = "SELECT * FROM a_surat_ijin a 
			INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) AND 
			( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR a.nomor_surat='{$_GET['key']}')
			ORDER BY surat_ijin_id DESC";
	else
	$query = "SELECT * FROM a_surat_ijin a
	INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
	INNER JOIN biodata c ON c.biodata_id = b.biodata_id  
	WHERE b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)  
	  ORDER BY  surat_ijin_id DESC
	 ";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=create&id='.$ey['surat_ijin_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=detail&tanggal='.$ey['tanggal'],
						'title'=>'Edit'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['surat_ijin_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'nomor'=>  strtoupper($ey['nomor_surat'] ),  
			'nik'=>  strtoupper($ey['nik'] ),  
			'nama'=>  strtoupper($ey['nama'] ),  
			'fd'=>  position_text_align( strtoupper($ey['full_day'] ),'center'),   
			'rec'=>  position_text_align(  $ey['rekomendasi'] ,'center'),   
			'dari'=>  position_text_align( date('H:i',strtotime($ey['jam_dari'] )),'center'),  
			'sampai'=>  position_text_align( date('H:i',strtotime($ey['jam_sampai'] )),'center'),  
			'keterangan'=>  position_text_align(  $ey['keterangan'] ,'center') ,
			'operasi'=> position_text_align(  $edit_button .' '. $delete_button  , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$menuontop = array(  
		'By Date'=>array('onclick'=>'javascript:location.href=\'index.php?com=ijin\''),
	);
	 
	$search = '<form method="GET">
	<input type="hidden" name="com" value="'.$_GET['com'].'" />
	<input type="hidden" name="task" value="'.$_GET['task'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search); 
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas , 9, false ,$paging);
}

function list_ijin_by_date($date){
my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor'=>array('style'=>'border-bottom:2px solid;width:12%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:8%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:27%'),  
		'Full Day'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'),  
		'Rekomendasi'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'),  
		'Dari'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'),  
		'Sampai'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'),  
		'Keterangan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:14%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM a_surat_ijin a
	INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
	INNER JOIN biodata c ON c.biodata_id = b.biodata_id  
	WHERE tanggal='".$date."' ORDER BY tanggal, surat_ijin_id DESC
	 ";
	$result = my_query($query );
	$row = array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=create&id='.$ey['surat_ijin_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=detail&tanggal='.$ey['tanggal'],
						'title'=>'Edit'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['surat_ijin_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'nomor'=>  strtoupper($ey['nomor_surat'] ),  
			'nik'=>  strtoupper($ey['nik'] ),  
			'nama'=>  strtoupper($ey['nama'] ),  
			'fd'=>  position_text_align( strtoupper($ey['full_day'] ),'center'),   
			'rec'=>  position_text_align(  $ey['rekomendasi'] ,'center'),   
			'dari'=>  position_text_align( date('H:i',strtotime($ey['jam_dari'] )),'center'),  
			'sampai'=>  position_text_align( date('H:i',strtotime($ey['jam_sampai'] )),'center'),  
			'keterangan'=>  position_text_align(  $ey['keterangan'] ,'center') ,
			'operasi'=> position_text_align(  $edit_button .' '. $delete_button  , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$menuontop = array(  
		'Kembali'=>array('onclick'=>'javascript:location.href=\'index.php?com=ijin&page='.$_GET['page'].'\''),
	);
	 
	$printmenu = additional_menu_on_list( $menuontop , ''); 
	return $printmenu .table_builder($header , $datas , 9, false );
}

function list_ijin(){

my_set_file_js(
	array(
		'components/system/jquery/combomulti/jquery.chainedSelects.js',
		'components/system/js/calendar/calendarDateInput.js' 
	)
);
	$periode_id=get_periode_absen() ;
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$ftanggal =  $periode['dari'] ;
	if($_GET['tanggal1']){ 
		$ftanggal = $_GET['tanggal1'];
	}else{
		list($yyyytanggal  , $mmtanggal , $ddtanggal  ) =explode("-" ,$periode['dari']) ;
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'. $yyyytanggal; 
	}
	$tanggal = array(
			'name'=>'tanggal1',
			'id'=>'tanggal1',
			'value'=>$ftanggal,
		);
		
	if($_GET['tanggal2']){ 		
		$ftanggal2 = $_GET['tanggal2'];
	}else{
		list($yyyytanggal  , $mmtanggal , $ddtanggal  ) =explode("-" ,$periode['sampai']) ;
		$ftanggal2 = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal; 
	}
	$tanggal2 = array(
			'name'=>'tanggal2',
			'id'=>'tanggal2',
			'value'=>$ftanggal2,
		);
	list($ddtanggal , $mmtanggal , $yyyytanggal  ) =explode("-" ,$ftanggal) ;
	list($ddtanggal2 , $mmtanggal2 , $yyyytanggal2  ) = explode("-" ,$ftanggal2 );
	$startDate = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal; 
	$endDate = $yyyytanggal2.'-'.$mmtanggal2.'-'.$ddtanggal2;

	$src ="<form method=\"GET\">
	<input type=\"hidden\" name=\"com\" value=\"{$_GET['com']}\" />
	<table border=\"0\" width=\"100%\"> <tr><td width='20%'>
	Tanggal&nbsp;:</td><td width='20%'>".form_calendar($tanggal)." </td><td width='5%'>s/d</td><td width='20%'> ".form_calendar($tanggal2)."</td><td width=\"10%\"> <input type=\"submit\" value=\"GO\" class=\"simple_search\"/></td> </tr></table></form>";
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nomor surat ijin'=>array('style'=>'border-bottom:2px solid;width:70%'),  
		'Jumlah Karyawan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);

	$query = "SELECT tanggal , COUNT(karyawan_id) AS jum FROM a_surat_ijin 
	WHERE DATE(tanggal) BETWEEN  '{$startDate}'  AND  '{$endDate}' 
	GROUP BY tanggal
	ORDER BY tanggal DESC,surat_ijin_id ASC
	 "; 
//if(isset($_GET['AA']) )print($query);
	 
	$result = my_query($query );
 
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}	
	if(isset($_GET['tanggal1']) && isset($_GET['tanggal2']) )	
		$pager_url  ="index.php?com={$_GET['com']}&tanggal1={$_GET['tanggal1']}&tanggal2={$_GET['tanggal2']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";
	else	 
		$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=tatib&task=edit&id='.$ey['tatib_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&page='.$current_page.'&task=detail_tanggal&tanggal='.$ey['tanggal'],
						'title'=>'Edit'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['tatib_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'tanggal'=>  strtoupper($ey['tanggal'] ),  
			'nomor'=>  daftar_nomor($ey['tanggal']),  
			'jumlah'=>  position_text_align(  $ey['jum'] ,'center') ,
			'operasi'=> position_text_align(   $detail_button  , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=ijin&task=create&id=0\''), 
		'By Nomor'=>array('onclick'=>'javascript:location.href=\'index.php?com=ijin&task=by_num\''),
	);
	$printmenu = additional_menu_on_list( $menuontop , $src);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  4, false,$paging );
} 

function daftar_nomor($date){
	$query = "SELECT nomor_surat FROM a_surat_ijin WHERE tanggal ='{$date}' ORDER by nomor_surat ASC";
	$result =my_query($query);
	$total = my_num_rows($result); 
	$datas = '';
	$i=0;
	while($row =my_fetch_array($result)){
		$i++;
		if($i <= 5){
			$datas .= $row['nomor_surat'].', ';
			if($i== 5 && $total > 6)$datas.='..., ';
		}
		elseif($i == $total)$datas .= $row['nomor_surat'].', ';
		else continue;
		
		
	}
	return rtrim( $datas,", ");
}


function form_surat_ijin( $id=0 ){
	my_set_file_js(
		array(
		'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);
	my_set_code_js(' 
		function getShift( ){
			var day = $(\'#tanggal_Day_ID\').val();
			var mon =  $(\'#tanggal_Month_ID\').val();
			var yer = $(\'#tanggal_Year_ID\').val();
			var  da = yer +\'-\'+ (parseFloat(mon)+parseFloat(1 ))+\'-\'+day;
			var name = $(\'#nama_karyawan\').val();
			$.get("get_karyawan_schedule.php", { nama: name , date:da ,check :1 },
			   function(data){
					var mySplitResult = data.split("|");  
					if(mySplitResult[3] == 1){
						alert(\'Golongan Jabatan Karyawan Tidak Dapat Lembur\');
					}
					$(\'#jadwal_kerja\').replaceWith(\'<span id="jadwal_kerja">\'+ mySplitResult[0] +\'</span>\');
					$(\'#realisasi_kerja\').replaceWith(\'<span id="realisasi_kerja">\'+ mySplitResult[1] +\'</span>\');
					$(\'#administrasi\').replaceWith(\'<span id="administrasi">\'+ mySplitResult[2] +\'</span>\');
					return false;
				 
			   });
		}	
	'); 	
	$fields=my_get_data_by_id('a_surat_ijin','surat_ijin_id',$id);
	
	
	
	$ijins = array();
	$query_ijin = "SELECT * FROM a_status_absen where jenis='I' ORDER BY initial_absen";
	$re = my_query($query_ijin);
	
	while($row = my_fetch_array($re) ){
		$ijins[$row['status_absen_id']] =$row['initial_absen'].' - '.$row['nama_absen'];
	}
	$ijin = array(
		'name'=>'status_absen_id',
		'id'=>'status_absen_id',
		'value'=>isset($_POST['status_absen_id']) ? $_POST['status_absen_id'] : $fields['status_absen_id'] 
		);
	$form_field_ijin = form_dropdown($ijin , $ijins).' <input type="text" name="jumlah_hari" size="1" value="1" /> hari';

	$viewx= '<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$viewx .= form_field_display( '<span id="bbbb">'.
	'<input type="checkbox" name="rekomendasi" value="Y" '.($fields['rekomendasi'] =='Y' ? ' checked="checked" ': "").' /> Rekomendasi '.'</span> &nbsp; &nbsp; &nbsp;'
	.'<input type="checkbox" name="potong_insentif" value="Y" '.($fields['potong_insentif'] =='Y' ? ' checked="checked" ': (!$fields ? ' checked="checked" ': '') ).' /> Potong insentif '.'</span>'
	, '<b><span id="aaaa"></span></b>');

	 
	$jamDari = array(
		'name'=>'jam_dari',
		'id'=>'jam_dari',
		'value'=>date('H:i',strtotime( ($fields ? $fields['jam_dari'] : '00:00:00'))),
	);
	$jam_dari = form_time($jamDari);
	$jamSampai = array(
		'name'=>'jam_sampai',
		'id'=>'jam_sampai',
		'value'=>date('H:i',strtotime(($fields ? $fields['jam_sampai'] : '00:00:00'))),
	);
	$jam_sampai = form_time($jamSampai);
	$viewx .= form_field_display( '<span class="label_form">'.$jam_dari.' &nbsp; s/d  &nbsp; '.$jam_sampai.'</span>'   , "<b>Waktu ijin</b>"    ); 
	
	
	$keterangan = array(
		'name'=>'keterangan',
		'id'=>'keterangan',
		'value'=>$fields['keterangan'],
	);
	$keterangan = form_textarea($keterangan);
	$viewx .= form_field_display($keterangan, "<b>Keterangan</b>" ); 
	$viewx .= '</table>';
 my_set_code_js('   
	$(document).ready(function() {
		  
		$("#kena_pajak2").change(function(){ 
		 if ($(this).is(":checked") ) { 
			$("#vmore").replaceWith("<div id=\'vmore\'>'.addslashes(str_replace("
","",$viewx)).'</div>");
			$("#pantext_pajak").replaceWith("<span id=\'pantext_pajak\'></span>");
		 } 
	   }); 
	   $("#kena_pajak1").change(function(){ 
		 if ($(this).is(":checked") ) { 
			$("#vmore").replaceWith("<div id=\'vmore\'></div>");
			$("#pantext_pajak").replaceWith("<span id=\'pantext_pajak\'>'.addslashes(str_replace("
","",$form_field_ijin)).'</span>");
		 } 
	   });
	});
	'
);
my_set_code_js('  
function findValue(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return   row[0];
}

function lookupAjax(){
	var oSuggest = $("#nama_karyawan")[0].autocompleter;
	oSuggest.findValue(); 
	return false;
}
$(document).ready(function() {
	$("#nama_karyawan").autocomplete(
		"autocomplete_daftar_karyawan.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	);
	 
});

function checkName(name){
	$.get("check_karyawan_names.php", { nama: name  },
	   function(data){
		 if(data ==  \'0\' ){
			alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
			$(\'#nama_karyawan\').val(\'\');
			return false;
		 } 
	   });
}
');
	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('a_surat_ijin','surat_ijin_id',$id);
	$karyawan = loaddata_karyawan( $fields['karyawan_id']);
	
	$form_nomor_surat = array(
		'name'=>'nomor_surat',
		'id'=>'nomor_surat',
		'readonly'=>'readonly',
		'value'=>isset($_POST['nomor_surat']) ?$_POST['nomor_surat'] : ($fields ? $fields['nomor_surat']: getNumber(date('Y'),date('m') )),
	);
	$fields_form_nomor = form_dynamic($form_nomor_surat);
	$view  .= form_field_display( '<span class="label_form">'.$fields_form_nomor.'</span>'   , "<b>Nomor surat ijin</b>"    ); 
	
	$ftanggal = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal; 
	}
	
	$tanggal = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
			'id'=>'tanggal',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal);
	$view .= form_field_display( $form_tanggal  , "<b>Tanggal</b>" );
	
	
	$full_days = array('Y'=>'Ya','T'=>'Tidak'); 
	$full_day = array(
		'name'=>'full_day',
		'id'=>'kena_pajak' ,
		'value'=>( $fields['full_day']  )
	); 
	$form_kena_pajak = form_radiobutton($full_day,$full_days) ;
	$view .= form_field_display(   $form_kena_pajak.'&nbsp; &nbsp; <span id=\'pantext_pajak\'>'.($fields['full_day'] == 'Y' ? $form_field_ijin: '' ) .'</span>'
	 , "<b>Full day</b>" );
	 
	$view .= '</table>';
	$view .= '<div id="vmore">'.($fields['full_day'] == 'T' ? $viewx : '' ) .'</div>';
	$view .= '<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	
	$tombol_check = array(
		'value' => ( 'Chk'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:getShift()',
		'class'=>'simple_button'
	);
	$form_tombol_check= form_dynamic($tombol_check); 
	
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : ($fields? $karyawan['nik'].'/'.$karyawan['nama'] :'') ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	$view .= form_field_display( $form_field_nama_karyawan/*.$form_tombol_check */ , "<b>Nama karyawan</b>"    ); 
	
	//$view .= form_field_display(  '<span id="jadwal_kerja"></span>' , "Jadwal kerja"    ); 
	//$view .= form_field_display(  '<span id="realisasi_kerja"></span>' , "Realisasi kerja"    ); 
	//$view .= form_field_display(  '<span id="administrasi"></span>' , "Administrasi"    ); 
	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$back = array(
		'value' => ( 'Kembali'),
		'name' => 'simpan', 
		'type'=>'button',
		'onclick'=>'javascript:location.href=(\'index.php?com=ijin&task='.( $id > 0 ? 'by_num' : 'by_date').'\')',
		'class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	
	$view .= form_field_display( $form_submit .' '.$form_back , "&nbsp;" ,  "" );

	$view .= form_footer( );
	return $view;
}
 

function form_surat_ijin_submit($id=0){ 
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
			list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
				$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	if(isset( $_POST['jumlah_hari'])){
		if( (int)$_POST['jumlah_hari'] > 0 ){
			for($n = 0; $n <= ((int)$_POST['jumlah_hari'] -1 ); $n++){

			$data_tanggal = date( "Y-m-d" , strtotime(  $ftanggal ) + ( $n * 24 * 60 * 60 ) );
				$datas = array(
					'nomor_surat'	=>my_type_data_str($_POST['nomor_surat']),
					'tanggal'	=>my_type_data_str($data_tanggal),
					'full_day'	=>my_type_data_str('Y'),
					'karyawan_id'	=>my_type_data_str($karyawan_id),
					'jam_dari'		=>my_type_data_str(set_post_time_data('jam_dari')),
					'jam_sampai'	=>my_type_data_str(set_post_time_data('jam_sampai')),
					'rekomendasi'	=>my_type_data_str(isset($_POST['rekomendasi']) ? 'Y' : 'T'),
					'potong_insentif'	=>my_type_data_str(isset($_POST['potong_insentif']) ? 'Y' : 'T'),
					'keterangan'	=>my_type_data_str($_POST['keterangan']),
					'status_absen_id'	=>my_type_data_int($_POST['status_absen_id']),
				);

				//if( $n  == 0){
				//	 my_update_record('a_surat_ijin' , 'surat_ijin_id', $id ,$datas);
				//}else{
					my_insert_record('a_surat_ijin' , $datas);
				//}	
			}
			return true;
		}
		return false;
	}
	
	$datas = array(
		'nomor_surat'	=>my_type_data_str($_POST['nomor_surat']),
		'tanggal'	=>my_type_data_str($ftanggal),
		'full_day'	=>my_type_data_str($_POST['full_day']),
		'karyawan_id'	=>my_type_data_str($karyawan_id),
		'jam_dari'		=>my_type_data_str(set_post_time_data('jam_dari')),
		'jam_sampai'	=>my_type_data_str(set_post_time_data('jam_sampai')),
		'rekomendasi'	=>my_type_data_str(isset($_POST['rekomendasi']) ? 'Y' : 'T'),
		'potong_insentif'	=>my_type_data_str(isset($_POST['potong_insentif']) ? 'Y' : 'T'),
		'keterangan'	=>my_type_data_str($_POST['keterangan']),
		'status_absen_id'	=>my_type_data_int($_POST['status_absen_id']),
	);

	if($id > 0){
			

		return my_update_record('a_surat_ijin' , 'surat_ijin_id', $id ,$datas);
	}
	return my_insert_record('a_surat_ijin' , $datas);
}



function ijin_validate( $id ){
	$errsubmit = false;
	$err = array();
	 
	 $karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
			list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
			$ftanggal	 = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	/*CHECK NOMOR SURAT*/
	if($karyawan_id == 0){
		$errsubmit = true;
		$err[] = "Nama karyawan tidak di temukan";	
	}
	
	if(!$ftanggal){
		$errsubmit = true;
		$err[] = "Tanggal tidak benar";	
	}
	
	$fullday = array('Y','T');
	if(! in_array($_POST['full_day'] , $fullday)){
		$errsubmit = true;
		$err[] = "Opsi full day belum dipilih";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}